package com.etop.weibo.dao;

import com.etop.weibo.beans.Comment;
import com.etop.weibo.dto.UserCommentDTO;
import java.util.List;

import com.etop.weibo.util.WeiBoException;
import com.etop.weibo.util.HibernateUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.Session;

public class CommentDao extends BaseDao<Comment>{

    private final static String ID = "id";
    private final static String USER_ID = "userId";
    private final static String SAY_ID = "sayId";
    private final static String COMMENT_ID = "commentId";
    private final static String PRAISE = "praise";
    private final static String WHAT = "what";
    private final static String CREATE_TIME = "createTime";

    public void deleteAll(int sayId) {
        Map<String, Object> params = new HashMap<>();
        params.put(SAY_ID, sayId);
        super.deleteByHQL(params);
    }

    public List<UserCommentDTO> list(int sayId) {
        Session session = HibernateUtil.openSession();
        List<Object[]> userComments = session.createSQLQuery("select c.u_id, u.nickname, c.id, c.praise, c.what, c.create_time"
                + " from t_user u right join"
                + " (select id, u_id, praise, what, create_time from t_comment com where s_id=:sayId) c"
                + " on c.u_id=u.id")
                .setInteger("sayId", sayId)
                .list();
        session.close();
        List<UserCommentDTO> list = new ArrayList<>();
        for (Object[] o : userComments) {
            UserCommentDTO dto = new UserCommentDTO((int) o[0], (String) o[1], (int) o[2], (int) o[3], (String) o[4], (Date) o[5]);
            list.add(dto);
        }
        return list;
    }

    public void praise(int id) {
        Session session = null;
        try {
            session = HibernateUtil.openSession();
            session.beginTransaction();
            session.createQuery("update Comment set praise=praise+1 where id=:id").setInteger("id", id).executeUpdate();
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session != null) {
                session.getTransaction().rollback();
            }
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    public void unPraise(int id) {
        Session session = null;
        try {
            session = HibernateUtil.openSession();
            session.beginTransaction();
            session.createQuery("update Comment set praise=praise-1 where id=:id").setInteger("id", id).executeUpdate();
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session != null) {
                session.getTransaction().rollback();
            }
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }
}
